<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="用户ID" prop="userId">
      <el-input v-model="dataForm.userId" placeholder="用户ID"></el-input>
    </el-form-item>
    <el-form-item label="店铺ID" prop="storeId">
      <el-input v-model="dataForm.storeId" placeholder="店铺ID"></el-input>
    </el-form-item>
    <el-form-item label="收件人名字" prop="buyerName">
      <el-input v-model="dataForm.buyerName" placeholder="收件人名字"></el-input>
    </el-form-item>
    <el-form-item label="收件人手机号" prop="buyerPhone">
      <el-input v-model="dataForm.buyerPhone" placeholder="收件人手机号"></el-input>
    </el-form-item>
    <el-form-item label="收件人地址" prop="buyerAddress">
      <el-input v-model="dataForm.buyerAddress" placeholder="收件人地址"></el-input>
    </el-form-item>
    <el-form-item label="订单总价格" prop="orderAmount">
      <el-input v-model="dataForm.orderAmount" placeholder="订单总价格"></el-input>
    </el-form-item>
    <el-form-item label="订单状态" prop="orderStatus">
      <el-input v-model="dataForm.orderStatus" placeholder="订单状态"></el-input>
    </el-form-item>
    <el-form-item label="支付状态" prop="payStatus">
      <el-input v-model="dataForm.payStatus" placeholder="支付状态"></el-input>
    </el-form-item>
    <el-form-item label="支付时间" prop="payTime">
      <el-input v-model="dataForm.payTime" placeholder="支付时间"></el-input>
    </el-form-item>
    <el-form-item label="订单送出时间" prop="sentTime">
      <el-input v-model="dataForm.sentTime" placeholder="订单送出时间"></el-input>
    </el-form-item>
    <el-form-item label="订单送到时间" prop="arrivedTime">
      <el-input v-model="dataForm.arrivedTime" placeholder="订单送到时间"></el-input>
    </el-form-item>
    <el-form-item label="订单完结时间" prop="finishTime">
      <el-input v-model="dataForm.finishTime" placeholder="订单完结时间"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          orderId: 0,
          userId: '',
          storeId: '',
          buyerName: '',
          buyerPhone: '',
          buyerAddress: '',
          orderAmount: '',
          orderStatus: '',
          payStatus: '',
          payTime: '',
          sentTime: '',
          arrivedTime: '',
          finishTime: ''
        },
        dataRule: {
          userId: [
            { required: true, message: '用户ID不能为空', trigger: 'blur' }
          ],
          storeId: [
            { required: true, message: '店铺ID不能为空', trigger: 'blur' }
          ],
          buyerName: [
            { required: true, message: '收件人名字不能为空', trigger: 'blur' }
          ],
          buyerPhone: [
            { required: true, message: '收件人手机号不能为空', trigger: 'blur' }
          ],
          buyerAddress: [
            { required: true, message: '收件人地址不能为空', trigger: 'blur' }
          ],
          orderAmount: [
            { required: true, message: '订单总价格不能为空', trigger: 'blur' }
          ],
          orderStatus: [
            { required: true, message: '订单状态不能为空', trigger: 'blur' }
          ],
          payStatus: [
            { required: true, message: '支付状态不能为空', trigger: 'blur' }
          ],
          payTime: [
            { required: true, message: '支付时间不能为空', trigger: 'blur' }
          ],
          sentTime: [
            { required: true, message: '订单送出时间不能为空', trigger: 'blur' }
          ],
          arrivedTime: [
            { required: true, message: '订单送到时间不能为空', trigger: 'blur' }
          ],
          finishTime: [
            { required: true, message: '订单完结时间不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.orderId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.orderId) {
            this.$http({
              url: this.$http.adornUrl(`/order/order/info/${this.dataForm.orderId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.userId = data.order.userId
                this.dataForm.storeId = data.order.storeId
                this.dataForm.buyerName = data.order.buyerName
                this.dataForm.buyerPhone = data.order.buyerPhone
                this.dataForm.buyerAddress = data.order.buyerAddress
                this.dataForm.orderAmount = data.order.orderAmount
                this.dataForm.orderStatus = data.order.orderStatus
                this.dataForm.payStatus = data.order.payStatus
                this.dataForm.payTime = data.order.payTime
                this.dataForm.sentTime = data.order.sentTime
                this.dataForm.arrivedTime = data.order.arrivedTime
                this.dataForm.finishTime = data.order.finishTime
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/order/order/${!this.dataForm.orderId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'orderId': this.dataForm.orderId || undefined,
                'userId': this.dataForm.userId,
                'storeId': this.dataForm.storeId,
                'buyerName': this.dataForm.buyerName,
                'buyerPhone': this.dataForm.buyerPhone,
                'buyerAddress': this.dataForm.buyerAddress,
                'orderAmount': this.dataForm.orderAmount,
                'orderStatus': this.dataForm.orderStatus,
                'payStatus': this.dataForm.payStatus,
                'payTime': this.dataForm.payTime,
                'sentTime': this.dataForm.sentTime,
                'arrivedTime': this.dataForm.arrivedTime,
                'finishTime': this.dataForm.finishTime
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
